package cate.game.server.rpc;

import cate.game.common.rpc.RpcListener4Game;
import cate.game.server.rpc.dispatch.RpcFafDispatcher;
import cate.game.server.rpc.dispatch.RpcRrDispatcher;
import cp.solution.business.game.conf.GameSrvEntity;
import cp.solution.fax.FaxTags;
import cp.solution.rpc.RpcMsgHead;
import cp.solution.rpc.listener.RpcFireAndForgetListener;
import cp.solution.rpc.listener.RpcRequestResponseListener;

public class RpcListener4GameImpl extends RpcListener4Game {

    public RpcListener4GameImpl(GameSrvEntity config) {
        super(config);
    }

    @Override
    public boolean accept(RpcMsgHead head) {
        if(head.hasTag(FaxTags.TAG_GAME_SRV_ID)) {
            if(!head.containsTag(FaxTags.TAG_GAME_SRV_ID, this.curSrvId())) return false;
        }
        if(head.hasTag(FaxTags.TAG_CROSS_CODE)) {
            if(!head.containsTag(FaxTags.TAG_CROSS_CODE, this.curCrossCode())) return false;
        }
        if(head.hasTag(FaxTags.TAG_GROUP_CODE)) {
            if(!head.containsTag(FaxTags.TAG_GROUP_CODE, this.curGroupCode())) return false;
        }
        return true;
    }

    private RpcFafDispatcher fafDispatcher = new RpcFafDispatcher();
    @Override
    public void onFireAndForget(String path, RpcFireAndForgetListener handler, int priority) {
        RpcListener4Game.regPath(path);
        fafDispatcher.addListener(path, handler, priority);
    }

    private RpcRrDispatcher rrDispatcher = new RpcRrDispatcher();
    @Override
    public void onRequestResponse(String path, RpcRequestResponseListener handler) {
        RpcListener4Game.regPath(path);
        rrDispatcher.addListener(path, handler);
    }

    @Override
    public void dispatchFireAndForget(RpcMsgHead head, byte[] msgBody) {
        fafDispatcher.dispatch(head, msgBody);
    }

    @Override
    public byte[] dispatchRequestResponse(RpcMsgHead head, byte[] msgBody) {
        return rrDispatcher.dispatch(head, msgBody);
    }
}
